package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsCoolToolErrorCode;
import com.macro.mall.tiny.modules.pms.model.PmsCoolToolErrorCode;
import com.macro.mall.tiny.modules.pms.service.PmsCoolToolBrandService;
import com.macro.mall.tiny.modules.pms.service.PmsCoolToolErrorCodeService;
import com.macro.mall.tiny.security.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 制冷错误码 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-25
 */
@RestController
@RequestMapping("/pms/pmsCoolToolErrorCode")
@Api(tags = "PmsCoolToolErrorCodeController")
@Tag(name = "PmsCoolToolErrorCodeController", description = "制冷错误码管理")
public class PmsCoolToolErrorCodeController {

    @Autowired
    private PmsCoolToolErrorCodeService pmsCoolToolErrorCodeService;

    @Autowired
    private PmsCoolToolBrandService pmsCoolToolBrandService;

    @ApiOperation("根据品牌ID获取错误码分类")
    @GetMapping("/getErrorCodeCategoryByBrandId")
    public CommonResult<List<PmsCoolToolErrorCodeVO>> getErrorCodeCategoryByBrandId(@RequestParam Integer brandId) {
        List<PmsCoolToolErrorCode> result = pmsCoolToolErrorCodeService.list(new QueryWrapper<PmsCoolToolErrorCode>()
                .select( "error_code").eq("brand_id", brandId)
                .groupBy("error_code"));
        if (result.isEmpty()) {
            return CommonResult.success(new ArrayList<>());
        }
        int i = 0;
        List<PmsCoolToolErrorCodeVO> last = new ArrayList<>();
        for (PmsCoolToolErrorCode pmsCoolToolErrorCode : result) {
            PmsCoolToolErrorCodeVO pmsCoolToolErrorCodeVO = new PmsCoolToolErrorCodeVO();
            pmsCoolToolErrorCodeVO.setErrorCode(pmsCoolToolErrorCode.getErrorCode());
            pmsCoolToolErrorCodeVO.setFirstLetter(CharUtil.getFirstLetter(pmsCoolToolErrorCode.getErrorCode()));
            pmsCoolToolErrorCodeVO.setShowStatus(i < 12 ? 1 : 0);
            i++;
            last.add(pmsCoolToolErrorCodeVO);
        }
        return CommonResult.success(last);
    }


    @ApiOperation("获取错误码详情")
    @GetMapping
    public CommonResult<List<PmsCoolToolErrorCode>> getErrorCodeDetailList(@RequestParam Integer brandId,
                                                                   @RequestParam String errorCode) {
        List<PmsCoolToolErrorCode> result = pmsCoolToolErrorCodeService.list(new QueryWrapper<PmsCoolToolErrorCode>()
                .eq("brand_id", brandId)
                .eq("error_code", errorCode));
        return CommonResult.success(result);
    }


    @ApiOperation("分页获取获取错误码列表(万能接口)" +
            "支持 id brandId errorCode adaptType title content 条件查询")
    @PostMapping("page")
    public CommonResult<IPage<PmsCoolToolErrorCode>> listErrorCode(@RequestBody PmsCoolToolErrorCode pmsCoolToolErrorCode
            , @ApiParam(defaultValue = "1") @RequestParam Integer pageNum, @ApiParam(defaultValue = "10") @RequestParam Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null)  pageSize = 10;

        QueryWrapper<PmsCoolToolErrorCode> query = new QueryWrapper<>();

        if (pmsCoolToolErrorCode.getId() != null)query.eq("id", pmsCoolToolErrorCode.getId());
        if (pmsCoolToolErrorCode.getBrandId() != null) query.eq("brand_id", pmsCoolToolErrorCode.getBrandId());
        if (pmsCoolToolErrorCode.getErrorCode() != null) query.eq("error_code", pmsCoolToolErrorCode.getErrorCode());
        if (pmsCoolToolErrorCode.getAdaptType() != null) query.like("adapt_type", pmsCoolToolErrorCode.getAdaptType());
        if (pmsCoolToolErrorCode.getTitle() != null) query.like("title", pmsCoolToolErrorCode.getTitle());
        if (pmsCoolToolErrorCode.getContent() != null) query.like("content", pmsCoolToolErrorCode.getContent());

        IPage<PmsCoolToolErrorCode> result = pmsCoolToolErrorCodeService.page(new Page<>(pageNum, pageSize, true), query);
        return CommonResult.success(result);
    }

    @ApiOperation("后台添加错误码")
    @PostMapping("/add")
    public CommonResult<Void> addErrorCode(@RequestBody PmsCoolToolErrorCode pmsCoolToolErrorCode) {
        pmsCoolToolErrorCode.setFirstLetter(CharUtil.getFirstLetter(pmsCoolToolErrorCode.getErrorCode()));
        pmsCoolToolErrorCodeService.save(pmsCoolToolErrorCode);
        return CommonResult.success(null);
    }

    @ApiOperation("后台修改错误码")
    @PutMapping("/update")
    public CommonResult<Void> updateErrorCode( @RequestBody PmsCoolToolErrorCode pmsCoolToolErrorCode) {
        pmsCoolToolErrorCodeService.updateById(pmsCoolToolErrorCode);
        return CommonResult.success(null);
    }

    @ApiOperation("后台批量删除错误码")
    @DeleteMapping("/delete")
    public CommonResult<Void> deleteErrorCode(@RequestParam("ids") List<Integer> ids) {
        pmsCoolToolErrorCodeService.removeByIds(ids);
        return CommonResult.success(null);
    }

    @Data
    public class PmsCoolToolErrorCodeVO {
        @ApiModelProperty(value = "错误码")
        private String errorCode;

        @ApiModelProperty(value = "首字母")
        private String firstLetter;

        @ApiModelProperty(value = "展示状态 0:普通 1:热门 默认为普通")
        private Integer showStatus;
    }
}

